package com.example.qixin0314.bean;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.qixin0314.Activity.NewsActivity;
import com.example.qixin0314.R;

import java.util.List;

public class newsAdapter extends RecyclerView.Adapter<newsAdapter.myHolder> {
    private List<newsModel>newsData;

    public void setNewsData(List<newsModel> newsData) {
        this.newsData = newsData;
    }
    private Context context;

    public newsAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public myHolder onCreateViewHolder(@NonNull  ViewGroup parent, int viewType) {
        return new myHolder(LayoutInflater.from(context).inflate(R.layout.news_item,null));
    }

    @Override
    public void onBindViewHolder(@NonNull  newsAdapter.myHolder holder, int position) {
        if (!newsData.get(position).getUrl().isEmpty()) {
            //with()方法创建对象
            Glide.with(context).load(newsData.get(position).getUrl()).into(holder.item_image);
        }
        else {
            holder.item_image.setImageResource(R.drawable.shouye);
        }
        holder.text_title.setText(newsData.get(position).getTitle());
        holder.text_time.setText(newsData.get(position).getTime());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, NewsActivity.class);
                intent.putExtra("name",newsData.get(position).getTitle());
                intent.putExtra("uri","http://android.qxswt.com/mediadetail/"+newsData.get(position).getId());
                context.startActivity(intent);
            }
        });


    }

    @Override
    public int getItemCount() {
        return null != newsData ? newsData.size() : 0;
    }

    class myHolder extends RecyclerView.ViewHolder{
        ImageView item_image;
        TextView text_title;
        TextView text_time;

        public myHolder(@NonNull View itemView) {
            super(itemView);
            item_image=itemView.findViewById(R.id.item_image);
            text_title=itemView.findViewById(R.id.text_title);
            text_time=itemView.findViewById(R.id.text_time);

        }
    }

}
